// 矩阵上面跑最短路
#include<bits/stdc++.h>
using namespace std;

typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;

const int N = 5010;
int mat[N][N], dist[N][N], rdir[] = {-1, 0, 1, 0}, cdir[] = {0, -1, 0, 1};

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  const int inf = 0x3f3f3f3f;
  memset(dist, inf, sizeof dist);
  for (int i=0;i<n;i++)
    for (int j=0;j<n;j++)
  cin >> mat[i][j];
  priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, greater<tuple<int, int, int>>> q;
  q.emplace(0, 0, 0);
  dist[0][0] = 0;

  while (!q.empty()) {
    int d, x, y;
    tie(d, x, y) = q.top();
    q.pop();
    for (int i = 0; i < 4; ++i) {
      int nx = x + rdir[i], ny = y + cdir[i];
      if (nx >= 0 && nx < n && ny >= 0 && ny < n) {
        int nd = d;
        if (mat[nx][ny]) ++nd;
        if (nd < dist[nx][ny]) {
          dist[nx][ny] = nd;
          q.emplace(nd, nx, ny);
        }
      }
    }
  }

  cout << dist[n - 1][n - 1] << endl;
  return 0;
}